﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VehicleManagement.Models;
using VehicleManagement.Codes;

namespace VehicleManagement.ControllersServices
{
    public class BreakRulesService
    {
        public IList<BreakRules> GetAllBreakRules()
        {
            return SqlMapperManager.Instance.Get().QueryForList<BreakRules>("SELECT_ALL_BREAKRULES", null);
        }

        public IList<BreakRules> GetAllBreakRulesEx()
        {
            return SqlMapperManager.Instance.Get().QueryForList<BreakRules>("SELECT_ALL_BREAKRULES_FOR_DISPLAY", null);
        }

        public object InsertBreakRules(BreakRules breakrules)
        {
            return SqlMapperManager.Instance.Get().Insert("INSERT_BREAKRULES", breakrules);
        }

        public object InsertBreakRulesEx(BreakRules breakrules)
        {
            convertBreakRulesForDatabase(ref breakrules);
            return InsertBreakRules(breakrules);
        }

        public int UpdateBreakRules(BreakRules breakrules)
        {
            return SqlMapperManager.Instance.Get().Update("UPDATE_BREAKRULES", breakrules);
        }

        public int UpdateBreakRulesEx(BreakRules breakrules)
        {
            convertBreakRulesForDatabase(ref breakrules);
            return UpdateBreakRules(breakrules);
        }

        public int DeleteBreakRules(int id)
        {
            return SqlMapperManager.Instance.Get().Delete("DELETE_BREAKRULES", id);
        }

        private void convertBreakRulesForDatabase(ref BreakRules breakRules)
        {
            CarInfoServices cis=new CarInfoServices();
            DriverServices ds = new DriverServices();
            breakRules.CarID = cis.GetCarIdByCarNum(breakRules.CarNumber);
            breakRules.DriverID = ds.GetDriverIdByDriverName(breakRules.DriverName);
        }

        public IList<BreakRules> SearchBreakRules(string carNumber, string driverName, string breakRules,string company)
        {
            return SqlMapperManager.Instance.Get().QueryForList<BreakRules>("SEARCH_BREAKRULES", new
            {
                CarNumber = carNumber,
                DriverName = driverName,
                BreakRules = breakRules,
                Company = company
            });
        }

        public IList<BreakRules> SearchBreakRules(string carNumber, string driverName, string breakRules, int skip, int max, string company)
        {
            return SqlMapperManager.Instance.Get().QueryForList<BreakRules>("SEARCH_BREAKRULES",
                new
                {
                    CarNumber = carNumber,
                    DriverName = driverName,
                    BreakRules = breakRules,
                    Company = company
                },skip,max);
        }
    }
}